﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace GreatB2C.Web.Manage.CRM.DataDictionary
{
    public partial class DictionaryManage : System.Web.UI.Page
    {
        private DataTable dtDictionary = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildTableSchema();
                LoadData();
            }
        }

        private void BuildTableSchema()
        {
            dtDictionary.Columns.Add("ID");
            dtDictionary.Columns.Add("DictValue");
            dtDictionary.Columns.Add("DefaultValue");
            dtDictionary.Columns.Add("Used");

            dtDictionary.Clear();
        }

        private void LoadData()
        {
            Business.Dictionary dictService = new GreatB2C.Business.Dictionary();
            GreatB2C.Model.Dictionary dictionary = dictService.ListDictionary("PE_Client", "Area");//todo

            if (dictionary != null)
            {
                if (dictionary.FieldValue.Trim() != "")
                {
                    string[] itemList = dictionary.FieldValue.Split(new char[] { '$' });
                    for (int i = 0; i < itemList.Length; i++)
                    {
                        if (itemList[i].IndexOf("|") > 0)
                        {
                            string[] dict = itemList[i].Split(new char[] { '|' });
                            DataRow dr = dtDictionary.NewRow();
                            dr["ID"] = i.ToString( );
                            dr["DictValue"] = dict[0];
                            dr["Used"] = dict[1];
                            dr["DefaultValue"] = dict[2];

                            dtDictionary.Rows.Add(dr);
                        }
                    }
                }

                for (int j = 0; j < 3; j++)
                {
                    DataRow dr = dtDictionary.NewRow();
                    dr["ID"]            = dtDictionary.Rows.Count.ToString();
                    dr["DictValue"]     = "";
                    dr["Used"]          = "0";
                    dr["DefaultValue"]  = "0";

                    dtDictionary.Rows.Add(dr);
                }

                gdvList.DataSource = dtDictionary;
                gdvList.DataBind();
            }  
        }

        protected void gdvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbDefaultValue");

                if (rb != null)
                    rb.Attributes.Add("onclick", "EnsureSingle(this)");
                if (rb != null && rb.Checked == true)
                    this.hdnLast.Value = rb.ClientID;
            }
        }

        protected void btnSaveSetting_Click(object sender, EventArgs e)
        {
            string fieldValue = string.Empty;
            foreach (GridViewRow row in gdvList.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    TextBox txtItemValue = (TextBox)row.FindControl("txtDictValue");
                    CheckBox chkUsed = (CheckBox)row.FindControl("chkUsed");
                    RadioButton rbDefaultValue = (RadioButton)row.FindControl("rbDefaultValue");

                    if (txtItemValue.Text.Trim() != string.Empty)
                    {
                        if (fieldValue.Trim() != string.Empty)
                        {
                            fieldValue += "$";
                        }

                        string item = txtItemValue.Text.Trim() + "|" + (chkUsed.Checked ? "1" : "0") + "|" + (rbDefaultValue.Checked ? "1" : "0");
                        fieldValue += item;
                    }
                }
            }

            if (fieldValue.Trim() != string.Empty)
            {
                Model.Dictionary dictionary = new GreatB2C.Model.Dictionary();
                dictionary.FieldName = "Area";// Request.QueryString["FieldName"].ToString();//todo
                dictionary.FieldValue = fieldValue;

                Business.Dictionary dictService = new GreatB2C.Business.Dictionary();
                bool result = dictService.UpdateFieldValue(dictionary);
                if (result == true)
                {
                    //todo
                }
                else
                {
                    //todo
                }
            }
        }
    }
}
